#include "StdAfx.h"
#include "SeqInput.h"
#include "HitKey.h"

namespace KeyAss 
{

	void SeqInput::SendBegin()
	{
		GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_ILANGUAGE, gLocaleInfo, sizeof(gLocaleInfo));
		_hkl = ::LoadKeyboardLayoutA( gLocaleInfo, KLF_ACTIVATE );
	}

	SeqInput::~SeqInput(void)
	{
	}

	SeqInput::SeqInput(XchangeKey* key)
	{
		_key = key;
		SendBegin();
	}

	void SeqInput::AddVK(BYTE vk, int rep)
	{
		for(int i = 0; i< rep; i++)
		{
			CtrlKey ck(this, vk);
		}
	}

	void SeqInput::AddText( const std::string& str )
	{
		if(!str.size()) return;
		bool isCtrl = false;
		for each( char c in str)
		{
			if(c == '$')
			{
				isCtrl = true;
				continue;
			}
			if(isCtrl)
			{
				bool ctrled = false;
				switch(c)
				{
				case 'U':{ CtrlKey ck(this, VK_UP); ctrled = true; }break;
				case 'V':{ CtrlKey ck(this, VK_DOWN); ctrled = true; }break;
				case 'L':{ CtrlKey ck(this, VK_LEFT); ctrled = true; }break;
				case 'R':{ CtrlKey ck(this, VK_RIGHT); ctrled = true; }break;
				case '<':{ CtrlKey ck(this, VK_RETURN); ctrled = true; }break;
				}
				if(ctrled) continue;
				CharKey charkey(this, '$');
			}
			CharKey charkey(this, c);
			isCtrl = false;
		}// End for
	}

	BOOL SeqInput::Send()
	{
		if( _hkl )
		{
			UnloadKeyboardLayout( _hkl );
		}
		return ::SendInput( _eq.size(), &_eq[0], sizeof( INPUT ));
	}

}//namespace